// pages/myIncome/myIncome.js
const app = getApp()
var api = require('../../utils/api.js')
var util = require('../../utils/util.js');
var currentShopId = 0;
var currentTime = app.globalData.global_current_time;
var currentType = 0;
var currentPage = 1;
var saleDetailLists = [];
var currentMachineId = 0;
var storeArr = [];
var machineArr = [];

Page({
  data: {
    page: 1,
    limit: 10,
    total_page: 0,
    times: ['累计', '今日', '本周', '本月', '本年'],
    screeningCol: true,
    currentTab: 0,
    endDate: "",
    startDate: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.totalMoney()
    this.incomeDetail()
  },
  onShow: function() {
    var now = new Date();
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var day = now.getDate();
    if (month < 10) {
      month = '0' + month;
    };
    if (day < 10) {
      day = '0' + day;
    };
    var formatDate = year + '-' + month + '-' + day;
    this.setData({
      endDate: formatDate,
      startDate: "请选择开始日期",
      ofEndDate: formatDate,
    })
  },
  // 收入累计
  totalMoney: function() {
    var that = this;
    api.POST({
      url: app.globalData.base_url + 'shop/incomeStatistics',
      params: {},
      success: function(res) {
        if (res.data.code == '') {
          that.setData({
            totalMoney: res.data.data,
          })
        }
      },
    })
  },

  // 收入明细
  incomeDetail: function() {
    var that = this;
    var a = /^(\d{4})-(\d{2})-(\d{2})$/
    if (that.data.currentTab>0){
      var start_time = ''
      var end_time = ''
    }else{
      if (a.test(this.data.startDate)) {
        var start_time = this.data.startDate;
      } else {
        var start_time = ''
      }

      if (a.test(this.data.endDate)) {
        var end_time = this.data.endDate;
      } else {
        var end_time = ''
      }
    }
    
    api.POST({
      params: {
        page: that.data.page,
        limit: that.data.limit,
        time: that.data.currentTab,
        start_time: start_time,
        end_time: end_time
      },
      url: app.globalData.base_url + 'shop/incomeDetail',
      success: function(res) {
        if (util.isEmpty(res.data.code)) {
          var paging = res.data.data.paging;
          var currentPage = paging.page;
          var totalPage = paging.total_page;
          var list = res.data.data.list;

          if (that.data.page == 1) {
            var itemData = list;
          } else {
            var itemData = that.data.incomeDetail.concat(list)
          }
          that.setData({
            hasMore: (currentPage >= totalPage) ? false : true,
            total_page: totalPage,
            incomeDetail: itemData
          })
        }
      },
      fail: function(fail) {
        util.showError("网络错误");
      }
    })
  },

  //加载更多
  loadMore: function() {
    if (this.data.page >= this.data.total_page) {
      return;
    }
    this.setData({
      page: this.data.page + 1
    });
    this.incomeDetail();
  },

  //刷新页面
  resetData: function() {
    var that = this
    that.setData({
      page: 1,
      hasMore: true
    });
    this.totalMoney();
    this.incomeDetail();
  },
  //页面下拉刷新
  onPullDownRefresh: function() {
    var that = this;

    setTimeout(function() {
      that.resetData()
    }, 1000)
    wx.stopPullDownRefresh();
  },

  //页面上拉触底
  onReachBottom: function(e) {
    this.loadMore();
  },
  //日期选择
  actionDayChoose: function(e) {
    this.setData({
      screeningCol: !this.data.screeningCol
    })
  },
  startDateChange: function(e) {
    var time = e.detail.value
    this.setData({
      startDate: time
    })
  },
  endDateChange: function(e) {
    // console.log(e);
    var time = e.detail.value
    this.setData({
      endDate: time
    })
  },
  //时间筛选
  confirm: function() {
    var a = /^(\d{4})-(\d{2})-(\d{2})$/
    if (a.test(this.data.startDate)) {
      var start_time = this.data.startDate;
    }else{
      var start_time =''
    }

    if (a.test(this.data.endDate)) {
      var end_time = this.data.endDate;
    }
    this.setData({
      currentPage: 1,
      start_time: start_time,
      end_time: end_time,
      screeningCol: true
    })
    this.refreshData();
  },
  cancel: function() {
    this.setData({
      screeningCol: true
    })
  },
  swichNav: function (e) {
    var now = new Date();
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var day = now.getDate();
    if (month < 10) {
      month = '0' + month;
    };
    if (day < 10) {
      day = '0' + day;
    };
    var formatDate = year + '-' + month + '-' + day;
    this.setData({
      endDate: formatDate,
      startDate: "请选择开始日期",
      ofEndDate: formatDate,
      currentTab: e.target.dataset.currentTime,
    })

    this.refreshData();
  },
  refreshData: function () {
    this.setData({
      page:1
    })
    this.incomeDetail()
  }
})